package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeLengthTjController;
import com.antoco.operation.bigscreen.model.dto.PipeLengthTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeLengthTj;
import com.antoco.operation.bigscreen.mapper.PipeLengthTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeLengthTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/pipe-length-tj")
@RestController
public class PipeLengthTjControllerImpl implements PipeLengthTjController {

    @Autowired
    private PipeLengthTjService pipeLengthTjService;

    @Autowired
    private PipeLengthTjMapper pipeLengthTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeLengthTjDTO save(@Validated @RequestBody PipeLengthTjDTO pipeLengthTjDTO) {
        PipeLengthTj pipeLengthTj = pipeLengthTjMapper.asEntity(pipeLengthTjDTO);
        pipeLengthTjService.save(pipeLengthTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeLengthTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PipeLengthTjDTO findById(@PathVariable("id") String id) {
        PipeLengthTj pipeLengthTj = pipeLengthTjService.findById(id).orElse(null);
        return pipeLengthTjMapper.asDTO(pipeLengthTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeLengthTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeLengthTjDTO> list() {
        return pipeLengthTjMapper.asDTOList(pipeLengthTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeLengthTjDTO> pageQuery(Pageable pageable) {
        Page<PipeLengthTj> pipeLengthTjPage = pipeLengthTjService.findAll(pageable);
        List<PipeLengthTjDTO> dtoList = pipeLengthTjPage
                .stream()
                .map(pipeLengthTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeLengthTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeLengthTjDTO update(@RequestBody PipeLengthTjDTO pipeLengthTjDTO, @PathVariable("id") String id) {
        PipeLengthTj pipeLengthTj = pipeLengthTjMapper.asEntity(pipeLengthTjDTO);
        pipeLengthTjMapper.asDTO(pipeLengthTjService.update(pipeLengthTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeLengthTjDTO;
    }
}